package org.springframework.postProcessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.AbstractBeanDefinition;

import java.util.Base64;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		// 获取数据源的bean的元数据
		AbstractBeanDefinition beanDefinition = (AbstractBeanDefinition)beanFactory.getBeanDefinition("user");
		MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();

		// 拿到密码
		Object value = propertyValues.getPropertyValue("name").getValue();

		if(value instanceof TypedStringValue name){
			// 使用base64进行编码
			String originalString = name.getValue();
			byte[] encodedBytes = Base64.getEncoder().encode(originalString.getBytes());
			String encodedString = new String(encodedBytes);
			propertyValues.addPropertyValue("name", encodedString);
		}


		System.out.println("-------");
	}
}
